<?php
$UserObj = new Customer();
$UnitObj = new Unit();

function show_menu(){
	$common_link = "?option=com_resman&user=owner&page=";
	$page = $_GET['page'];
	
	include_once 'views/show_menu.html.php';
}

function display_home(){
	global $UnitObj, $KToolBar;
	$manager = identify_owner();
	$units = $UnitObj->find(array("owner_id"=>$manager->id));
	include_once 'views/main_menu.html.php';
}

function display_account(){
	global $manager, $UserObj, $KToolBar;
	$manager = $UserObj->find((int)$manager->id);
	$MailingAddrObj = new MailingAddr();
	$MailingAddr = $MailingAddrObj->find(array("owner_id" => $manager->id));
	$MailingAddr = $MailingAddr[0];
	
	include_once 'views/account.html.php';	
}

function show_new_unit_page(){
	global $KToolBar;
	include_once 'views/new_unit.html.php';
}

function save_new_unit(){
	global $UnitObj, $manager, $notice;
	if($_POST['unit']['is_hotel']) $_POST['unit']['is_hotel'] = 1;
	if($_POST['unit']['is_group']) $_POST['unit']['is_group'] = 1;
	$_POST['unit']['owner_id'] = $manager->id;
	
	if($_POST['unit']['address'] == ""){
		$notice['error'] = 1;
		$notice['msg'] = "The Address field is required";
	}
	else if($_POST['unit']['title'] == ""){
		$notice['error'] = 1;
		$notice['msg'] = "The Title field is required";
	}
	else {
		$save_unit_msg = $UnitObj->insert_or_update($_POST['unit'],"");
		$notice['error'] = 0;
		$notice['msg'] = "The unit has been created. Now you can <a href=\"?option=com_resman&user=owner&page=property_details&uid=".mysql_insert_id()."\">edit</a> the details of this unit.";
	}
}

function save_mailing_addr(){
	global $notice, $UserObj, $manager;
	$notice['error'] = 1;
	
	mysql_query("BEGIN");
	$notice = $UserObj->insert_or_update($_POST['manager'], array("id"=>$manager->id));
	if($notice['error'] == 0){
		$MailingAddrObj = new MailingAddr();
		$count = $MailingAddrObj->count("SELECT * FROM `".$MailingAddrObj->get_table_name()."` WHERE `owner_id` = '$manager->id'");
		if($count>0){
			$notice = $MailingAddrObj->insert_or_update($_POST['manager_mailing'], array("owner_id"=>$manager->id));
		}
		else {
			$_POST['manager_mailing']['owner_id'] = $manager->id;
			$notice = $MailingAddrObj->insert_or_update($_POST['manager_mailing'], "");
		}
	}
	if( $notice['error'] == 0 ){
		mysql_query("COMMIT");
		$notice['msg'] = "Your account information has been saved";
	}
	else {
		mysql_query("ROLLBACK");
		$notice['msg'] = "There is some error while updating your information";
	}
}

function display_property_details(){
	global $UnitObj, $KToolBar;
	$CategoriesObj = new Category();
	
	$unit = $UnitObj->find((int)$_GET['uid']);
	$categories = $CategoriesObj->find("all");
	include_once 'views/property_details.html.php';
}

function display_listing(){
	global $UnitObj, $KToolBar;
	$ImagesObj = new Image();
	
	$unit = $UnitObj->find((int)$_GET['uid']);
	$images = $ImagesObj->select_row_query("SELECT * FROM `".$ImagesObj->get_table_name()."` WHERE `uid` = '$unit->id' ORDER BY `order` ASC", "");
	include_once 'views/listing.html.php';
}

function display_images(){
	global $UnitObj, $KToolBar;
	$ImagesObj = new Image();
	
	$unit = $UnitObj->find((int)$_GET['uid']);
	$images = $ImagesObj->select_row_query("SELECT * FROM `".$ImagesObj->get_table_name()."` WHERE `uid` = '".$_GET['uid']."' ORDER BY `order` ASC", "");
	include_once 'views/images.html.php';
}

function display_amenities(){
	global $UnitObj, $KToolBar;
	
	$unit = $UnitObj->find((int)$_GET['uid']);
	$unit->amenities = explode(",", $unit->amenities);
	
	$AmenitiesObj = new Amenity();
	$amenities = $AmenitiesObj->find("all");
	include_once 'views/amenities.html.php';
}

function display_rates(){
	global $UnitObj, $KToolBar;
	$PricesObj = new Price();
	$prices = $PricesObj->select_row_query("SELECT * FROM `".$PricesObj->get_table_name()."` WHERE `uid` = '".$_GET['uid']."' ORDER BY `startdate` ASC", "");
	$prices_count = count($prices);
	
	$unit = $UnitObj->find((int)$_GET['uid']);
	include_once 'views/rates.html.php';	
}

function display_calender(){
	global $UnitObj, $KToolBar;
	
	$unit = $UnitObj->find((int)$_GET['uid']);
	include_once 'views/calender.html.php';
}

function save_property_details(){
	global $UnitObj, $notice;
	$notice['error'] = 1;
	
	if( empty($_POST['units']['address']) ){
		$notice['msg'] = "The address field cannot be left blank";
	}
	else{
		$notice = $UnitObj->insert_or_update($_POST['units'], array('id'=>$_GET['uid']));
	}
}

function save_listing(){
	global $UnitObj, $notice;
	$notice['error'] = 1;
	
	if( empty($_POST['units']['title']) ){
		$notice['msg'] = "The Listing Title field cannot be left blank";
	}
	else{
		$notice = $UnitObj->insert_or_update($_POST['units'], array('id'=>$_GET['uid']));
	}
}

function image_uploader(){
	global $max_images_per_unit, $UnitObj, $notice;
	$ImagesObj = new Image();
	
	// unit address will not be displayed as there is no reference to Unit
	$uid = $_GET['uid'];
	$input = "upload_image";
	$target_path = ADMIN_COMPONENT_PATH.COMPONENT_PATH."uploads/";
	$uploaded_image = upload_image($input, $target_path);

	$images_count = $ImagesObj->count("SELECT * FROM `".$ImagesObj->get_table_name()."` WHERE `uid` = $uid");
	if($images_count < $max_images_per_unit){
		if($uploaded_image['error'] != 1){
			$unit_images_msg = $ImagesObj->insert_row_query("INSERT INTO `".$ImagesObj->get_table_name()."` VALUES(null, '$uid', '".COMPONENT_PATH."uploads/".$uploaded_image['modified_name']."', '".COMPONENT_PATH."uploads/thumbnails/thumbnail_".$uploaded_image['modified_name']."', '".COMPONENT_PATH."uploads/original_imgs/".$uploaded_image['modified_name']."', '', 1)");
			$inserted_id = mysql_insert_id();
			$unit_images_msg = $ImagesObj->insert_row_query("UPDATE `".$ImagesObj->get_table_name()."` SET `order` = '".$inserted_id."' WHERE `id` = '".$inserted_id."'");
			$uploaded_image['id'] = $inserted_id;
			$uploaded_image['order'] = $inserted_id;
		}
	}
	else{
		$uploaded_image['error'] = 1;
		$uploaded_image['msg'] = "You cannot upload more than ". $max_images_per_unit ." images per unit.";
	}

	$notice = $uploaded_image;
}

function move_image(){
	global $notice, $UnitObj;
	$uploaded_image['error'] = 1;

	$ImagesObj = new Image();
	$order1 = $_POST['order1'];
	$order2 = $_POST['order2'];
	$query = "UPDATE `".$ImagesObj->get_table_name()."` SET `order`= CASE WHEN `order`= '$order1' THEN '$order2' WHEN `order`= '$order2' THEN '$order1' END WHERE `order` IN ($order1, $order2)";
	$notice = $ImagesObj->insert_row_query($query);
}

function delete_image(){
	global $UnitObj, $notice;
	$notice['error'] = 1;
	
	$ImagesObj = new Image();
	$image_id = $_POST['delete_image'];
	$image = $ImagesObj->select_row_query("SELECT `uid` FROM `".$ImagesObj->get_table_name()."` WHERE `id` = '$image_id'", 1);
	if($image->uid != $_GET['uid']){
		$notice['msg'] = "";
	}
	else{
		$notice = $ImagesObj->delete_row_query("DELETE FROM `".$ImagesObj->get_table_name()."` WHERE `id` = '".$image_id."'");
		if($notice['error'] == 0) $notice['msg'] = "The selected image has been deleted";
	}
}

function save_amenities(){
	global $UnitObj, $notice;
	$notice['error'] = 1;
	if( in_array( $_GET['uid'], get_unit_ids() ) ){
		$AmenitiesObj = new Amenity();
		
		$amenities = "";
		foreach ($_POST['amenities'] as $amenity_id){
			$amenities .= $amenity_id.",";
		}
		$amenities = substr($amenities,0,-1);
		$notice = $UnitObj->insert_or_update( array("amenities"=>$amenities), array("id"=>$_GET['uid']) );
	}
	else $notice['msg'] = "You cannot modify the selected unit information";
}

function save_rates(){
	global $notice;
	$notice['error'] = 1;
	$PricesObj = new Price();
	$uid = $_GET['uid'];
	
	$_POST['prices']['startdate'] = array_filter($_POST['prices']['startdate']);
	$_POST['prices']['enddate'] = array_filter($_POST['prices']['enddate']);
	$_POST['prices']['price'] = array_filter($_POST['prices']['price']);
	
	mysql_query("BEGIN");
	$to_update = count($_POST['prices']['id']);
	for($i = 0; $i < $to_update; $i++){
		$price = filter_var($_POST['prices']['price'][$i], FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
		$update_prices_msg = $PricesObj->insert_row_query("UPDATE `".$PricesObj->get_table_name()."` SET `startdate` = '".get_date_from_str($_POST['prices']['startdate'][$i])."', `enddate` = '".get_date_from_str($_POST['prices']['enddate'][$i])."', `price` = '".$_POST['prices']['price'][$i]."' WHERE `id` = '".$_POST['prices']['id'][$i]."'");
		if($update_prices_msg['error'] == 1) break;
	}
	$to_insert = count($_POST['prices']['startdate']);
	for($i = $to_update; $i < $to_insert; $i++){
		$insert_prices_msg = $PricesObj->insert_row_query("INSERT INTO `".$PricesObj->get_table_name()."` SET `uid` = '".$uid."', `startdate` = '".get_date_from_str($_POST['prices']['startdate'][$i])."', `enddate` = '".get_date_from_str($_POST['prices']['enddate'][$i])."', `price` = '".$_POST['prices']['price'][$i]."'");
		if($insert_prices_msg['error'] == 1) break;
	}
	if( $update_prices_msg['error'] == 1 || $insert_prices_msg['error'] == 1 ){
		mysql_query("ROLLBACK");
		echo $update_prices_msg['error']."-2nd<br/>".$insert_prices_msg['error']."-3rd<br/>";
		$notice['error'] = 1;
		$notice['msg'] = $edit_units_msg['msg']."<br/>".$update_prices_msg['msg']."<br/>".$insert_prices_msg['msg'];
	}
	else{
		mysql_query("COMMIT");
		$notice['error'] = 0;
		$notice['msg'] = "Unit information updated successfully";
	}
}

function delete_price_range(){
	global $notice;
	$notice['error'] = 1;
	$PricesObj = new Price();
	
	$price_range_id = $_POST['price_range'];
	$valid = false;
	$ranges = $PricesObj->select_row_query("SELECT `id` FROM `".$PricesObj->get_table_name()."` WHERE `uid` = '".$_GET['uid']."'", "");
	foreach ($ranges as $range){
		if( $range->id == $price_range_id ){
			$valid = true;
			break;
		}
	}
	if( $valid ){
		$notice = $PricesObj->delete_row_query("DELETE FROM `".$PricesObj->get_table_name()."` WHERE `id` = '$price_range_id'");
		$notice['msg'] = "The price range has been deleted";
	}
	else{
		$notice['msg'] = "Some error occured while deleting the price range. Possible violation of authorization.";
	}
}

function save_blocked_dates(){
	global $UnitObj, $notice;
	$notice['error'] = 1;
	
	$notice = $UnitObj->insert_row_query("UPDATE `".$UnitObj->get_table_name()."` SET `blocked_dates` = '".$_POST['blocked_dates']."' WHERE `id` = '".$_GET['uid']."'");
	if($notice['error'] == 0) $notice['msg'] = "The selected dates have been blocked";
}
?>